﻿using SHRIS.MIS.Data.Repository;
using SHRIS.MIS.Entity.ScadaData;
using SHRIS.MIS.Service.OpcService;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SHRIS.MIS.Service.Device
{
    public class BasicDevice
    {
        
        protected readonly WorkshopCommunicationService _workshopCommunicationService;
        public BasicDevice(DeviceEntity deviceEntity, WorkshopCommunicationService workshopCommunicationService)
        {
            DeviceEntity = deviceEntity ?? throw new ArgumentNullException(nameof(deviceEntity));
            _workshopCommunicationService = workshopCommunicationService ?? throw new ArgumentNullException(nameof(workshopCommunicationService));
        }

        public DeviceEntity DeviceEntity { get; }

        public event EventHandler<DeviceValueChangedEventArgs> PropertyChanged;

        protected void OnPropertyChanged(string propertyName, object previewValue, object currentValue)
        {
            PropertyChanged?.Invoke(this, new DeviceValueChangedEventArgs(DeviceEntity, propertyName, previewValue, currentValue));
            //PropertyChanged?.BeginInvoke(this, new DeviceValueChangedEventArgs(DeviceEntity, propertyName, previewValue, currentValue), null, null);
        }

        public bool SetParameter<T>(string properName, T value)
        {
            bool result = false;
            PropertyInfo property = GetType().GetProperty(properName);
            if (property != null)
            {
                try
                {
                    property.SetValue(this, value, null);
                    result = true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return result;
        }

        public object GetParameter(string properName)
        {
            PropertyInfo property = GetType().GetProperty(properName);

            return property.GetValue(this, null);
        }

        public string GetDeviceCode()
        {
            return DeviceEntity.DeviceCode;
        }

        public string GetDeviceName()
        {
            return DeviceEntity.DeviceName;
        }

        public int GetDeviceTypeCode()
        {
            return DeviceEntity.DeviceTypeCode;
        }

        public virtual void InitalDevice()
        {
            //await Task.Delay(10);
            throw new Exception("Device not initialized");
        }

    }


    public class DeviceValueChangedEventArgs : EventArgs
    {
        public DeviceEntity DeviceEntity { get; }
        public string PropertyName { get; }
        public object PreviewValue { get; }
        public object CurrentValue { get; }
        public DeviceValueChangedEventArgs(DeviceEntity deviceEntity, string propertyName, object preValue, object currentValue)
        {
            DeviceEntity = deviceEntity;
            PropertyName = propertyName;
            PreviewValue = preValue;
            CurrentValue = currentValue;
        }
    }




}
